package com.oneinlet.component;

/**
 * Created by WangZiHe on 2017/11/5
 * QQ/WeChat:648830605
 * QQ-Group:368512253
 * Blog:www.520code.net
 * Github:https://github.com/yancheng199287
 */
public class JsonResult {

    public enum Status {

        OK(200, ""),
        FAILED(400, "业务逻辑错误"),
        SERVER_ERROR(500, "接口错误"),
        NOT_FOUND(404, "接口未找到"),
        FORBIDDEN(403, "禁止访问"),
        UNAUTHORIZED(401, "未经授权");

        public int code;
        public String desc;

        Status(int code, String desc) {
            this.code = code;
            this.desc = desc;
        }
    }

    public static Result getSuccessResult(Object data) {
        return new Result(Status.OK.code, Status.OK.desc, data);
    }

    public static Result getFailResult(String message) {
        return new Result(Status.FAILED.code, message, null);
    }

    public static Result getFailResult(Status status, String message) {
        return new Result(status.code, message, null);
    }


    //注意对于fast json 必须要有空的构造方法，也就是在这里的属性必须赋默认值并且访问权限是protected以上，gson不需要
    public static class Result {
        public int code;
        public String msg;
        public Object data;

        public Result() {
        }

        private Result(int code, String msg, Object data) {
            this.code = code;
            this.msg = msg;
            this.data = data;
        }
    }
}
